package com.xz.springcloud.service.impl;

import com.xz.springcloud.dao.OrderDao;
import com.xz.springcloud.domain.Order;
import com.xz.springcloud.service.AccountService;
import com.xz.springcloud.service.OrderService;
import com.xz.springcloud.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author：xz
 * @Date：2023/3/20
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderDao orderDao;
    @Resource
    private StorageService storageService;
    @Resource
    private AccountService accountService;

    /**
     * 创建订单->调用库存服务扣减库存->调用账户服务扣减账户余额->修改订单状态
     * 简单说：下订单->扣库存->减余额->改状态
     */
    @Override
    @GlobalTransactional(name = "order",rollbackFor = Exception.class)
    public void create(Order order) {

        //1、 新建订单
        log.info("---->开始新建订单");
        orderDao.create(order);

        //2、 扣减库存
        log.info("---->订单微服务开始调用库存，做扣减库存开始");
        storageService.decrease(order.getProductId(), order.getCount());
        log.info("---->订单微服务开始调用库存，做扣减开粗结束");

        //3、 扣减账户余额
        log.info("---->订单微服务开始调用账户，做扣减钱开始");
        accountService.decrease(order.getUserId(), order.getMoney());
        log.info("---->订单微服务开始调用账户，做扣减钱结束");

        //4、 修改订单状态 ，从0到1，1代表已完成
        log.info("---->修改订单状态开始");
        orderDao.update(order.getUserId(), 0);
        log.info("---->修改订单状态结束");

        log.info("---->下订单结束了，O(∩_∩)O哈哈~");

    }
}
